import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public final class SessionAuthServlet extends HttpServlet
{
  protected void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException
  {
    sendPage(req, res, req.getSession(false));
  }

  protected void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException
  {
    if(req.getParameter("login") != null)
    {
      HttpSession session = req.getSession(true);
      String name = req.getParameter("name");
      if(name == null || name.length()==0) name = "Anonymous";
      session.putValue("name", name);
      sendPage(req, res, session);
    }
    else
    {
      HttpSession session = req.getSession(false);
      if(session != null) session.invalidate();
      sendPage(req, res, null);
    }
  }

  private void sendPage(HttpServletRequest req, HttpServletResponse res,
			HttpSession session)
          throws ServletException, IOException
  {
    res.setContentType("text/html");
    res.setHeader("pragma", "no-cache");

    PrintWriter o = res.getWriter();
    o.print("<HTML><HEAD><TITLE>SessionAuthServlet</TITLE></HEAD><BODY>");
    if(session == null)
      o.print("<FORM METHOD=POST>Please enter your name: "+
	      "<INPUT TYPE=TEXT NAME=\"name\">"+
	      "<INPUT TYPE=SUBMIT NAME=\"login\" VALUE=\"Log in\">"+
	      "</FORM></BODY></HTML>");
    else
      o.print("Hi " + session.getValue("name") +
	      "<P><FORM METHOD=POST><INPUT TYPE=SUBMIT NAME=\"logout\" "+
	      "VALUE=\"Log out\"></FORM>" +
	      "<form method=\"POST\" action=\"http://localhost:8080/TestProject/AddCart\" "
	    +"<table><p><font size=\"5\">G3 Shopping Cart</font></p>" 
		+"<tr><td><p><font size=\"4\">Sports :</font></p></td></tr>" 
		+"<tr><td><p>&nbsp;&nbsp;&nbsp; 1: Complete Cricket&nbsp; kit&nbsp;($100)</td>"
		+"<td><input type=\"checkbox\" name=\"T1\" value=\"ON\"></td> "
		+"<td>Quantity<input type=\"text\" name=\"T2\" size=\"7\"></p></td></tr>"
		+"<tr><td><p>&nbsp;&nbsp;&nbsp; 2: Complete Soccer&nbsp; kit ($100)</td>"
		+"<td><input type=\"checkbox\" name=\"T3\" value=\"ON\"></td>" 
		+"<td> Quantity <input type=\"text\" name=\"T4\" size=\"7\"></p></td></tr>" 
		+"<tr><td><p><font size=\"4\">Perfumes :</font></p></td></tr>"
		+"<tr><td><p>&nbsp;&nbsp;&nbsp; 1: Blue Jeans($50)</td>" 
		+"<td><input type=\"checkbox\" name=\"T5\" value=\"ON\"></td> " 
		+"<td>Quantity<input type=\"text\" name=\"T6\" size=\"7\"></p></td></tr>"
		+"<tr><td><p>&nbsp;&nbsp;&nbsp; 2: Polo Sport($80)</td>"
		+"<td><input type=\"checkbox\" name=\"T7\" value=\"ON\"></td>" 
		+"<td>Quantity <input type=\"text\" name=\"T8\" size=\"7\"></p></td></tr>" 
		+"<tr><td><p><font size=\"4\">Cigarettes :</font></p></td></tr>" 
		+"<tr><td><p>&nbsp;&nbsp;&nbsp; 1: Marlboro($10)</td>" 
		+"<td><input type=\"checkbox\" name=\"T9\" value=\"ON\"></td>" 
		+"<td>Quantity <input type=\"text\" name=\"T10\" size=\"7\"></p></td></tr>" 
		+"<tr><td><p>&nbsp;&nbsp;&nbsp; 2: Dunhill($10)</td>" 
		+"<td><input type=\"checkbox\" name=\"T11\" value=\"ON\"></td>" 
		+"<td>Quantity <input type=\"text\" name=\"T12\" size=\"7\"></p></td><blockquote>  <blockquote><blockquote></tr>" 
		+"<tr><td><p><input type=\"submit\" value=\"Add to Cart\" name=\"B3\"></p>    </blockquote></blockquote></blockquote></td></tr></table></form></BODY></HTML>");
    o.close();
  }
}